package com.nineclock.document.service.Impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.nineclock.common.enums.ResponseEnum;
import com.nineclock.common.exception.NcException;
import com.nineclock.common.threadlocal.CurrentUserHolder;
import com.nineclock.common.utils.BeanHelper;
import com.nineclock.document.dto.DocFileHistoryDto;
import com.nineclock.document.mapper.DocFileHistoryMapper;
import com.nineclock.document.pojo.DocFileHistory;
import com.nineclock.document.service.DocService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class DocServiceImpl implements DocService {
    @Autowired
    private DocFileHistoryMapper docFileHistoryMapper;

    /**
     * 查询历史记录
     * @param fileId 文档id
     * @return
     */
    @Override
    public List<DocFileHistoryDto> queryDocFileHistory(Integer fileId) {
        if (fileId == null){
            throw new NcException(ResponseEnum.FILE_NOT_FOUND);
        }
        LambdaQueryWrapper<DocFileHistory> lambdaQW = new LambdaQueryWrapper<>();
        lambdaQW.eq(DocFileHistory::getFileId,fileId);
        List<DocFileHistory> docFileHistories = docFileHistoryMapper.selectList(lambdaQW);
        String username = CurrentUserHolder.get().getUsername();

        List<DocFileHistoryDto> dtos = BeanHelper.copyWithCollection(docFileHistories, DocFileHistoryDto.class);

        for (DocFileHistoryDto dto : dtos) {
            dto.setUserName(username);
        }
        return dtos;
    }
}
